package my.jwds.p869;

import java.util.*;

public class ReorderedPowerOf2 {

    public static void main(String[] args) {
        ReorderedPowerOf2 obj = new ReorderedPowerOf2();
        System.out.println(obj.reorderedPowerOf2(635824465));

    }
    List<int[]>[] map;
    public ReorderedPowerOf2(){
        map = init(9);
    }
    public boolean reorderedPowerOf2(int n) {
        String nStr = n+"";
        int numLen = nStr.length();
        List<int[]> list = map[numLen];
        if (list == null)return false;
        int [] compare = numberToArray(n);
        for (int[] ints : list) {
            if (Arrays.equals(ints,compare)){
                return true;
            }
        }
        return false;
    }

    public List<int[]>[] init(int size){
        long num = 1L;
        List<int[]>[] res = new List[size+1];
        int numLen;
        while((numLen = (num+"").length()) <= size){
            List<int[]> list = res[numLen];
            if (list == null){
                list = new ArrayList<>();
                res[numLen] = list;
            }
            int[] pos = numberToArray(num);
            list.add(pos);
            num*=2;
        }
        return res;
    }
    public int[] numberToArray(long num){
        int[] pos = new int[10];
        while(num != 0){
            pos[(int) (num%10)] ++;
            num/=10;
        }
        return pos;
    }
 }